package com.spring.arch.uaa.oauth2.handler;

import com.spring.arch.uaa.oauth2.bean.SecurityUser;
import com.spring.arch.uaa.oauth2.bean.SecurityUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 认证Token增强
 *
 * @author sunyx
 */
@Component
public class AuthTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Object principal = authentication.getPrincipal();
        Map<String, Object> additionalInfo = new HashMap<>(10);
        if(principal instanceof SecurityUser){
            SecurityUser user = (SecurityUser)principal;

            additionalInfo.put("userId", user.getId());
            additionalInfo.put("account", user.getAccount());
            additionalInfo.put("mobile", user.getMobile());
        }

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);

        return accessToken;
    }

}